package com.witmore.neutron.ai.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jk.common.response.ResponseResult;
import com.witmore.neutron.ai.db.entity.AgentDO;
import com.witmore.neutron.ai.db.entity.AgentPublishConfigDO;
import com.witmore.neutron.ai.db.entity.ModelConfigDO;
import com.witmore.neutron.ai.db.mapper.AgentPublishConfigMapper;
import com.witmore.neutron.ai.web.controller.vo.*;
import com.witmore.neutron.ai.web.service.RobotService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Description: RobotController
 * Author: 豆子高
 * Date: 2025/5/15 15:03
 */
@Slf4j
@Controller
@RequestMapping("/robot")
public class RobotController {

    @Value("${witmore.neutron-ai.domain}")
    private String WITMORE_NEUTRON_AI_DOMAIN;

    @Autowired
    private RobotService robotService;
    @Autowired
    private AgentPublishConfigMapper agentPublishConfigMapper;

    @PostMapping("/list")
    public String list(Model model, @RequestBody RobotQueryParam robotQueryParam) {
        List<AgentResult> list = robotService.list(robotQueryParam);
        model.addAttribute("agentList", list);
        return "dashboard";
    }

    @GetMapping("/detail/{agentCode}")
    public String robotDetail(@PathVariable("agentCode") String agentCode, Model model) {
        AgentDO agent = robotService.getAgentByCode(agentCode);
        if (agent == null) {
            model.addAttribute("chatError", "Robot not found");
            return "chatError";
        }
        model.addAttribute("agent", agent);
        // 获取模型列表
        List<ModelConfigDO> modelList = robotService.getModels().getData();
        model.addAttribute("modelList", modelList);
        // 发布信息
        List<AgentPublishConfigDO> agentPublishConfigDOS = agentPublishConfigMapper.selectList(new LambdaQueryWrapper<AgentPublishConfigDO>()
                .eq(AgentPublishConfigDO::getAgentCode, agent.getAgentCode())
        );
        AtomicReference<AgentPublishConfigDO> apiConfig = new AtomicReference<>(new AgentPublishConfigDO());
        // 发布地址组装
        agentPublishConfigDOS.forEach(agentPublishConfigDO -> {
            agentPublishConfigDO.setAgentShowTitle(StringUtils.isEmpty(agentPublishConfigDO.getAgentShowTitle())
                    ? agent.getAgentName() : agentPublishConfigDO.getAgentShowTitle());
            String publishUrl = agentPublishConfigDO.getPublishUrl();
            if (publishUrl != null && !publishUrl.isEmpty()) {
                agentPublishConfigDO.setPublishUrl(WITMORE_NEUTRON_AI_DOMAIN + agentPublishConfigDO.getPublishUrl());
            }
            if (agentPublishConfigDO.getPublishType() == 1){
                apiConfig.set(agentPublishConfigDO);
            }
        });
        model.addAttribute("publishList", agentPublishConfigDOS);
        // 获取API的配置数据
        model.addAttribute("apiConfig", apiConfig.get());
        return "robot-detail";
    }

    @ResponseBody
    @PostMapping("/create")
    public ResponseResult<Void> create(@RequestBody AgentCreateParam createParam) {
        return robotService.createRobot(createParam);
    }

    @ResponseBody
    @GetMapping("/getModels")
    public ResponseResult<List<ModelConfigDO>> getModels() {
        return robotService.getModels();
    }

    @ResponseBody
    @PostMapping("/update")
    public ResponseResult<Void> update(@Valid @RequestBody RobotUpdateParam param) {
        log.warn("update param:{}", param);
        return robotService.update(param);
    }

    @ResponseBody
    @PostMapping("/updateName")
    public ResponseResult<Void> updateName(@Valid @RequestBody RobotUpdateParam param) {
        return robotService.updateName(param);
    }

    @ResponseBody
    @PostMapping("/updatePublishStatus")
    public ResponseResult<Void> updatePublishStatus(@Valid @RequestBody AgentPublishConfigParam param) {
        return robotService.updatePublishStatus(param);
    }

    @ResponseBody
    @PostMapping("/updateAgentShowTitle")
    public ResponseResult<Void> updateAgentShowTitle(@Valid @RequestBody AgentPublishConfigParam param) {
        return robotService.updateAgentShowTitle(param);
    }

    @ResponseBody
    @PostMapping("/delete")
    public ResponseResult<Void> delete(@RequestParam("agentId") Integer agentId) {
        return robotService.deleteRobot(agentId);
    }

    @ResponseBody
    @PostMapping("/lock")
    public ResponseResult<Void> lock(@RequestParam("agentId") Integer agentId) {
        return robotService.lock(agentId);
    }

    @ResponseBody
    @PostMapping("/unlock")
    public ResponseResult<Void> unLock(@RequestParam("agentId") Integer agentId) {
        return robotService.unlock(agentId);
    }

    @ResponseBody
    @PostMapping("/copy")
    public ResponseResult<Void> copy(@RequestParam("agentId") Integer agentId) {
        return robotService.copy(agentId);
    }
}
